package com.xinsoft.controller.purchase;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinsoft.common.ApprovedParam;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.*;
import com.xinsoft.entity.po.SysUser;
import com.xinsoft.entity.vo.ScmApproveVo;
import com.xinsoft.entity.vo.ScmPurchasePlanPageVo;
import com.xinsoft.entity.vo.ScmPurchasePlanVo;
import com.xinsoft.service.ApproveService;
import com.xinsoft.service.ScmPurchasePlanService;
import com.xinsoft.service.SysUserService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.utils.TokenUtil;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 采购计划
 * 1、发起申请：新增采购申请、审批节点记录表、审批人记录
 * 2、审批/转派：审批--新增、更新审批节点记录表、审批人记录表
 * 3、结案审批：反写采购申请表、节点记录表、审批人记录表
 */
@Slf4j
@RestController
@RequestMapping("/scm/purchasePlan")
public class ScmPurchasePlanController {

    @Autowired
    private ScmPurchasePlanService scmPurchasePlanService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ApproveService approveService;

    /**
     * 暂存采购计划单
     */
    @PostMapping("/addOrUpdate")
    public Response<?> addOrUpdate(@RequestBody ScmPurchasePlanDto purchasePlanDto) {
        // 参数校验
        ValidatorUtils.validateEntity(purchasePlanDto);
        purchasePlanDto.setPurchasePlanType(Const.ApproveType.CGJH);
        purchasePlanDto.setDoClose(0);
        purchasePlanDto.setPurchasePlanStatus(Const.ApproveStatus.STAGING);
        return scmPurchasePlanService.addOrUpdate(purchasePlanDto);
    }

    /**
     * 提交采购计划单
     */
    @PostMapping("/submintApprove")
    public Response<?> submintApprove(@RequestBody ScmPurchasePlanDto purchasePlanDto) {
        // 参数校验
        ValidatorUtils.validateEntity(purchasePlanDto);
        purchasePlanDto.setPurchasePlanType(Const.ApproveType.CGJH);
        purchasePlanDto.setDoClose(0);
        purchasePlanDto.setPurchasePlanStatus(Const.ApproveStatus.SUBMIT);
        return scmPurchasePlanService.submintApprove(purchasePlanDto);
    }

    /**
     * 审批采购计划单
     */
    @PostMapping("/approved")
    public Response<?> approved(@Validated @RequestBody ApprovedParam param) {
        log.info("采购计划单-审批参数：{}", JSON.toJSONString(param));
        Response<?> approved = approveService.approved(param, Const.ApproveType.CGJH);
        return approved;
    }

    /**
     * 删除计划申请单
     */
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Long> ids) {
        return scmPurchasePlanService.del(ids);
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody ScmPurchasePlanPageParam scmPurchasePlanPageParam) {
        scmPurchasePlanPageParam.setUserId(TokenUtil.getUserId());
        if (ObjectUtil.isNotEmpty(scmPurchasePlanPageParam.getType()) && scmPurchasePlanPageParam.getType().equals(4)){
            List<Integer> usersOfDept = sysUserService.list(new LambdaQueryWrapper<SysUser>().eq(SysUser::getDeptId, TokenUtil.getDeptId()))
                    .stream().map(SysUser::getId).collect(Collectors.toList());
            scmPurchasePlanPageParam.setUsersOfDept(usersOfDept);
        }
        return scmPurchasePlanService.findPageListByParam(scmPurchasePlanPageParam);
    }

    /**
     * 获取采购计划单详情
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        ScmPurchasePlanVo scmPurchasePlan = scmPurchasePlanService.getScmPurchase(id);
        scmPurchasePlan.setScmApproveHisList(approveService.approvalHistory(id, Const.ApproveType.CGJH));
        return Response.succeed(scmPurchasePlan);
    }

    /**
     * 采购计划单分页列表(导出)
     */
    @PostMapping("/export")
    public Response<?> export(@Valid @RequestBody ScmPurchasePlanPageParam scmPurchasePlanPageParam, HttpServletResponse response) throws Exception {
        scmPurchasePlanPageParam.setUserId(TokenUtil.getUserId());
        List<ScmPurchasePlanPageVo> planVos = scmPurchasePlanService.findListByParam(scmPurchasePlanPageParam);
        ExcelUtils.exportByEasypoi(planVos, ScmPurchasePlanPageVo.class, "采购计划单", "采购计划单", response, "采购计划单");
        return Response.succeed();
    }
}
